var express = require('express');
var router = express.Router();
// const orderModel = require("../model/orderModel.js");
const orderService = require('../src/services/orderService');

/* GET home page. */


// router.get('/getorder', function(req, res, next) {

//   let a=orderModel.find().exec()
//   a.then(
//     (data)=>{
//      res.send(data);
//    })
// });


router.get('/', async function (req, res) {
    let { type, value, current, pageSize } = req.query;
    let searchParams = {};
    searchParams[type] = value;
    searchParams[type] = { $regex: value };
    console.log(searchParams[type]);
    let data = await orderService.find({ current: current || 1, pageSize: pageSize || 5 }, searchParams);
    console.log(data);
    res.send(data);
});
router.get('/:id', async function (req, res) {
    let { id } = req.params;
    let data = await orderService.findById(id);
    res.send(data);
});

router.delete('/:id', async function (req, res) {
    let { id } = req.params;
    let data = await orderService.delete(id);
    res.send(data);
});

router.put('/:id', async function (req, res) {
    // let b = {times:"3",userId:"5",phone:"3",address:"5",orderId:"3",status:["3","3","3"]}
    let { id } = req.params;
    let { times, userId, phone, address, orderId, status } = req.body
    // req.body = b;
    console.log({ _id: id, times, userId, phone, address, orderId, status })
    let data = await orderService.update({ _id: id, times, userId, phone, address, orderId, status });
    res.send(data);
});

module.exports = router;